//指针

#include<stdio.h>
#include<string.h>//字符串相关
#include<stdlib.h>//内存操作

int main(){

    int a=1025;
    int* p=&a;
    printf("%d %d\n",p,&a);//代表a的地址
    printf("%d\n",&p);
    printf("%d\n",*p);//解引用，代表地址a上的值
    *p=8;           //指针改值
    printf("%d\n",a);

    int b=20;
    *p=b;
    printf("%d %d\n",p,*p);  //a的地址不变，值变为20

    //指针的计算
    printf("%d\n",p);
    printf("%d\n",sizeof(int));
    printf("%d\n",p+1); 
    printf("%d\n",*(p+1));  //没有值，为随机值

    char* p0; 
    p0=(char*)p;  //强制转换 //1025=00000000 00000000 00000100 00000001
    printf("%d\n",*p);
    printf("%d\n",*p0); //int=4byte char=1byte 保留00000001部分
    printf("%d\n",*(p0+1));                      //00000100
    
    多重指针，画内存条

    指针作为函数参数-->pointer2.c
    数组作为函数参数-->pointer2.c


    指针与字符数组#include<string.h>
    单个字符用' ';字符串用" "
    char C[20];
    C[0]='J';
    C[1]='o';
    C[2]='y';
    C[3]='\0';
    printf("%s",C);

    char C[]="JOHN"; //未指明数组大小时，必须进行初始化
    char D[]={'J','O','H','N','\0'}; //显示表达
    printf("%d\n",sizeof(C));
    printf("%d\n",strlen(C)); //不包含NULL字符

    char Ch[]="Hello";
    char* p=Ch;
    printf("%c\n",p[1]);
    p[0]='A';
    printf("%s\n",Ch);


    //指针与数组 
    int A[]={1,2,3,4,5};
    int* p=A;
    printf("%d %d %d\n",A,p,&A[0]);
    printf("%d %d %d\n",A+1,p+1,&A[1]); //地址

    printf("%d %d %d\n",*A,*p,A[0]);
    printf("%d %d %d\n",*(A+1),*(p+1),A[1]); //值

    //指针与二维数组
    int A[2][3]={{1,2,3},{4,5,6}};
    int(*p)[3]=A;
    printf("%d %d %d\n",A,p,&A[0]);      
    printf("%d %d %d\n",A+1,p+1,&A[1]);

    printf("%d %d %d %d\n",*A,*p,A[0],&A[0][0]);
    printf("%d %d %d %d\n",*(A+1),*(p+1),A[1],&A[1][0]); 
    printf("%d %d %d %d\n",*(A+1)+2,*(p+1)+2,A[1]+2,&A[1][2]); 
    printf("%d\n",*(*A+1));   //2

    //指针与多维数组 A[i][j]=*(B[i]+j)=*(*(B+i)+j)

    //指针与动态内存—dynmaic memory    栈stack；堆heap
    /*#include<stdlib.h>
    向heap申请内存指令：
    malloc-void* malloc(size_t size);
    calloc-void* calloc(size_t num,size_t size);  会对其初始化为0；
    realloc-void* realloc(void* ptr,size_t size);
    */
    int n;
    printf("please enter the size of array:\n");
    scanf("%d",&n);
    int* p1=(int*)malloc(n*sizeof(int));
    *p1=10;
    free(p1);
    int* p2=(int*)calloc(n,sizeof(int));
    free(p2);
    int* p3=(int*)realloc(p1,2*n*sizeof(int));
    //int* p3=(int*)realloc(p1,0);//=free(p1);
    //int* p3=(int*)realloc(NULL,n*sizeof(int))//=malloc操作
    printf("p1'address is:%d\np3'address is:%d\n",p1,p3); //对p1内存地址进行扩展
    free(p3);




    



    


}